<?php

	class Users_model extends CI_Model 
	{
		public function __construct()
		{
			parent::__construct();
			 
			// Load database
			$this->load->database();
		}
		
		// Add users
		public function add($email, $password, $firstname = NULL, $lastname = NULL, $twitter_id = NULL)
		{
			$values = array(
				'email' => $email,
				'password' => $password,
				'firstname' => $firstname,
				'lastname' => $lastname,
				'twitter_id' => $twitter_id
			);
			
			$this->db->insert('users', $values); 
			return $this->db->insert_id();
		}
		
		// Get all users
		public function get_all()
		{
			$query = $this->db->get('users');
			
			if($query->num_rows() > 0)
			{
				return $query->row();
			}
			else
			{
				return NULL;
			}
		}
		

		// Return single user record as an object
		public function get($ID)
		{
			$query = $this->db->get_where('users', array('ID' => $ID));
			
			if($query->num_rows() > 0)
			{
				return $query->row();
			}
			else
			{
				return NULL;
			}
		}
		
		// Return single user record as an object
		public function get_by_email($email)
		{
			$query = $this->db->get_where('users', array('email' => $email));
			
			if($query->num_rows() > 0)
			{
				return $query->row();
			}
			else
			{
				return NULL;
			}
		}
		
		// Return single user record as an object
		public function get_by_twitter_id($twitter_id)
		{
			$query = $this->db->get_where('users', array('twitter_id' => $twitter_id));
			
			if($query->num_rows() > 0)
			{
				return $query->row();
			}
			else
			{
				return NULL;
			}
		}
		
		// Update user
		public function update($object)
		{
			if($object->ID)
			{
				$this->db->where('ID', $object->ID);
				$this->db->update('users', $object); 
			}
		}
		
	}

?>